$(function(){
    $(".forward").on("click",function(){
        history.back(-1);
    });
    var intoDetailFlag=1;//能否跳转进入到详情页面
    var url=SYS_GLOBAL_CONFIG.reqUrl,
    asyn="true",
    type="post";
    var userId="";
    //获取当前用户Id
    if(window.localStorage){
        userId=parseInt(localStorage.userId);
    }else{
        userId=parseInt(SYS_GLOBAL_CONFIG.getQueryString("userId"));
        alert("请打开浏览器cookie功能,或者更换浏览器");
        console.log("当前浏览器不支持本地localStorage");
    }
    var address=$("#address").html();
    var productCollect=JSON.stringify({
        "code":"getUserCollectList",
        "version":"1.0",
        "data":{
            "collectType":"PRODUCT",
            "userId":userId,
            "currentPage":1,
            "showCount":10
        }
    });
    var shopCollect=JSON.stringify({
        "code":"getUserCollectList",
        "version":"1.0",
        "data":{
            "collectType":"SHOP",
            "userId":userId,
            "currentPage":1,
            "showCount":10
        }
    });
    var serviceCollect=JSON.stringify({
        "code":"getUserCollectList",
        "version":"1.0",
        "data":{
            "collectType":"SERVICE",
            "userId":userId,
            "currentPage":1,
            "showCount":10
        }
    });
    function getData(url,requestData,call_func,asyn,type){
        $.ajax({
            url:url,
            type:type,
            datatype:"json",
            contentType:"application/json",
            data:requestData,
            async:asyn,
            success:function(data){
                call_func(data);  
            },
            error:function(msg){
                location.href="./error.html";
                console.log("异常："+msg);
            }   
        });        
    }
    //商品
    function call_func1(data){
       console.log(data);
       var datas=data.data.page.datas;
       var lens=datas.length;
        if(lens<1){
            $("#detail-no").addClass("show").removeClass("hide");
        }else{
             $("#detail-no").addClass("hide").removeClass("show");
        }
        var dataStr1="";
        if(datas.length>0){
            for(var i=0,len=datas.length;i<len;i++){
                var isauth="";
               dataStr1+='<div data-proId="'+datas[i].productId+'" class="zwl-noCertificate"><div class="goodNoCertificate"><img class="zwl-img" src="'+datas[i].fullPath+'" alt=""><div class="zwl-shoppingDetail"><span class="zwl-p">'+datas[i].productName+'</span><img data-proId="'+datas[i].productId+'" class="chooseImg hide" data-flag="0" src="./images/xuanzhong.png" alt=""><span class="shop1">'+datas[i].pdTypeName+'</span><span class="shopRe1">'+isauth+'</span><span class="span2"> <span class="money">'+datas[i].saleprice+'元/</span><span class="cess">'+datas[i].unit+'</span></span></div></div></div>';
           }
       }
        $("#detailgoods").html(dataStr1);
        $(".zwl-noCertificate,.zwl-Certificate").on("click",fns1); 
   }
    //服务
    function call_func2(data){
        console.log(data);
        var datas2=data.data.page.datas;
        var len2=datas2.length;
        if(len2<1){
            $("#detail-no").addClass("show").removeClass("hide");
        }
        var liStr="";
        for(var i=0;i<len2;i++){
            var pic="";
            var fulladdress=datas2[i].address;
            var addr=fulladdress.split(" ");
            var imgsLen=datas2[i].serviceImgs.length;
            if(imgsLen>0){
                for(var j=0;j<imgsLen;j++){
                    if(j===3){
                        break;
                    }
                    pic+='<img class="l"  src="'+datas2[i].serviceImgs[j].FULLPATH+'" alt="">'
                 }
            }else{
                pic="";
            }

            if(datas2[i].userAuthStatus==1){
                img1='<img class="img-v" src="./images/vip_icon.png" alt="">';

            }else{
                img1=""; 
            }

            liStr+='<div class="s-detail" data-productId="'+datas2[i].productId+'">'+
                    '<aside class="l-aside l">'+
                        '<img src="'+datas2[i].userImgPath+'" alt="">'+
                    '</aside>'+
                    '<aside class="r-aside l">'+
                        '<div class="raside-name clear">'+
                            '<div class="pro l">'+
                                '<p class="pro-name">'+datas2[i].productName+'</p>'+
                                 '<i>'+datas2[i].saleprice+'/'+datas2[i].unit+'</i>'+
                                '<p class="user-name"><span class="small-light">'+datas2[i].userName+'</span>'+img1+'<span class="zwl-as">'+addr[addr.length-2]+'/'+addr[addr.length-1]+'</span></p>'+
                            '</div>'+

                            '<div class="clears"></div>'+
                        '</div>'+
                        '<p class="raside-explain">'+datas2[i].remark+'</p>'+
                        '<div class="raside-img">'+pic+'<div class="clears"></div>'+
                        '</div>'+
                        '<div class="raside-detail ">'+
                               '<span class="detail-info">'+datas2[i].parentServiceName+'</span>'+
                               '<img class="mag-l20" src="./images/shoucang.png" alt=""><span class="detail-info">'+datas2[i].serviceCollectCount+'</span>'+
                               '<img class="mag-l20"src="./images/liulan.png" alt=""><span class="detail-info">'+datas2[i].viewCount+'</span>'+
                        '</div>'+
                    '</aside>'+
                    '<img data-productId="'+datas2[i].productId+'" class="chooseImg hide" data-flag="0" src="./images/xuanzhong.png" alt="">'+
                    '<div class="clears"></div>'+
                '</div>';    
        }
        $(".serviceDetail").html(liStr);
        $(".currentpage").html(data.data.currentPage);
        $(".s-detail").on("click",fns2);     
    }
    //商铺
    function call_func3(data){
        console.log(data);
        var img="";
        var datas3=data.data.page.datas;
        var len3=datas3.length;
        if(len3<1){
            $("#detail-no").addClass("show").removeClass("hide");
        }
        var li="";
        for(var i=0;i<len3;i++){
           
            li+='<div class="detail-shop" data-shopId="'+datas3[i].shopId+'">'+
                ' <img class="img2" src="'+datas3[i].logoImgPath+'" alt="">'+
                '<div class="introduce">'+
                ' <div class="introduce-detail">'+
                '<div class="introduce-name">'+
                '<span class="span-detail"><span class="span-name">'+datas3[i].shopName+'</span>'+ 
                ''+img+'</span> '+
                ' <span class="cess r1"></span><span class="regstr r1"></span> </div>'+
                '<div class="introduce-tag">'+
                '<img data-shopId="'+datas3[i].shopId+'" class="chooseImg hide" data-flag="0"'+
                'src="images/xuanzhong.png" alt=""></div>'+
                '<div class="clears"></div></div></div><div class="clears"></div></div>'   
        }
        $("#detailshops").html(li);
        $(".currentpage").html(data.data.currentPage);
        $(".detail-shop").on("click",fns3);
    }
     //商铺
    function fns3(){
        var shopId =$(this).attr("data-shopId");
        location.href= "./shopDetail.html?shopId="+shopId+"&userId="+userId;//此处拼接内容
    }
    //服务
    function fns2(){
        var productId=$(this).attr("data-productId");
        location.href="./serviceDetail.html?productId="+productId+"&userId="+userId;
    }
    //商品
    function fns1(){
        var proId=$(this).attr("data-proId");
        location.href="./goodDetail.html?productId="+proId+"&userId="+userId;
    }
    //获取收藏产品信息
    getData(url,productCollect,call_func1,asyn,type);
    //获取收藏服务信息
    getData(url,serviceCollect,call_func2,asyn,type);
    //获取收藏商家信息
    getData(url,shopCollect,call_func3,asyn,type);
    /**
     *--------收藏夹编辑-------
     **/
    
    var curr_page=1;//默认当前为第一页(1商品，2店铺，3服务)
    var collect={
        goods:{},
        shops:{},
        services:{}
    };
    //设置一个布尔值，判断是否存在数据
    collect.goods.isExist=false;
    collect.shops.isExist=false;
    collect.services.isExist=false;

    /**
     *tab切换不同的收藏夹
     **/
    $("#series .series-span").on("click",sortTabs); 
    function sortTabs(){
        editFlag=1;
        $("#cancelCollect").addClass("hide").removeClass("show");
        //将选择图片全部设置成hide，并且默认为未选中状态
$("#detailshops,#detailservices,#detailgoods").find(".chooseImg").attr("src","./images/xuanzhong.png").addClass("hide").removeClass("show");

        $(".edit-s").hide().siblings().css("display","block");
        var dataStr=$(this).attr("data-sort");
        var isExist;
        var ids="detail"+dataStr;
        var $ids="#"+ids;
        console.log($ids);
        if($ids=="#detailgoods"){
            curr_page=1;
        }else if($ids=="#detailshops"){
            curr_page=2;
        }else if($ids=="#detailservices"){
            curr_page=3;
        }else{
            console.log("出现异常");   
        }    
//            $($ids).siblings().find(".chooseImg").addClass("hide");
        console.log("当前"+$($ids).html().trim());
        if($($ids).html().trim()==""){
            $("#detail-no").addClass("show").removeClass("hide");
        }else{
            $("#detail-no").addClass("hide").removeClass("show");
        }
        $($ids).addClass("show").removeClass("hide").siblings(".detail-series").addClass("hide").removeClass("show");
        $(this).addClass("bt36c").siblings().removeClass("bt36c");
        if(dataStr.indexOf("goods")>-1){
            isExist=collect.goods.isExist+"goods";
        }else if(dataStr.indexOf("shops")>-1){
            isExist=collect.shops.isExist+"shops";
        }else if(dataStr.indexOf("services")>-1){
            isExist=collect.services.isExist+"services";
        }else{
            console.log("数据获取失败");
        }
$($ids).siblings().find(".chooseImg").addClass("hide").removeClass("show");//切换的时候将所有的重置成空的
        $("#cancelCollect").addClass("hide").removeClass("show").addClass("bggray").removeClass("bgred");
    }
    var editFlag=1;//(1代表着编辑，0代表着完成)

    function setBind(ele,fnc){
        $(ele).off("click",fnc);
        $(ele).on("click",function(){
            var _t=this;
            fncImg(_t);
        });
    }
    function fncImg(_th){ 
        var _this=_th;
        var imgSrc=$(_this).find(".chooseImg").attr("src");
        if(imgSrc.indexOf("xuanzhong_pre")>-1){
            $(_this).find(".chooseImg").attr("src","./images/xuanzhong.png").attr("data-flag","0");
            var eleLen=$(_this).siblings().length;
            if(eleLen>0){
                if($(_this).siblings().find(".chooseImg").attr("src").indexOf("xuanzhong_pre")==-1){
                    $("#cancelCollect").addClass("bggray").removeClass("bgred");
                }else{
                    $("#cancelCollect").addClass("bgred").removeClass("bggray");
                }
            }else{
                $("#cancelCollect").addClass("bggray").removeClass("bgred");       
            }
        }else{
            $("#cancelCollect").addClass("bgred").removeClass("bggray"); $(_this).find(".chooseImg").attr("src","./images/xuanzhong_pre.png").attr("data-flag","1");
            }         
    }
    function cancelBind(ele,fnc){
        $(ele).off("click",function(){
            var _t=this;
            fncImg(_t)
            console.log("++++++++++++");
        });
        $(ele).on("click",fnc);   
    }
    //编辑
    var edit=function (_this){
        $(".detail-shop,.s-detail,.zwl-noCertificate").off("click");
        var sort="shops";
        $(_this).hide().siblings().css("display","block");
        if(editFlag){//编辑
            setBind(".detail-shop",fns3);
            setBind(".s-detail",fns2);
            setBind(".zwl-noCertificate",fns1);
            console.log("当前标志1:"+editFlag);
            $("#cancelCollect").addClass("show").removeClass("hide");
            editFlag=0;
            if($("#detailshops").attr("class").indexOf("show")>-1){        $("#detailshops").find(".chooseImg").addClass("show").removeClass("hide").siblings().find(".chooseImg").addClass("hide").removeClass("show");    
            }else  if($("#detailservices").attr("class").indexOf("show")>-1){
                $("#detailservices").find(".chooseImg").addClass("show").removeClass("hide").siblings().find(".chooseImg").addClass("hide").removeClass("show");    
            }else if($("#detailgoods").attr("class").indexOf("show")>-1){
                $("#detailgoods").find(".chooseImg").addClass("show").removeClass("hide").siblings().find(".chooseImg").addClass("hide").removeClass("show");    
            }else{

            }
        }else{//完成
            console.log("当前标志0:"+editFlag);
            cancelBind(".detail-shop",fns3);
            cancelBind(".s-detail",fns2);
            cancelBind(".list,.list-little",fns1);
            $("#cancelCollect").addClass("hide").removeClass("show");
            //将选择图片全部设置成hide，并且默认为未选中状态
$("#detailshops,#detailservices,#detailgoods").find(".chooseImg").attr("src","./images/xuanzhong.png").addClass("hide").removeClass("show");
            editFlag=1;
        }
        /**
         *删除收藏的id值
         **/
        var pro_ids=[];//商品
        var ser_ids=[];//服务
        var shop_ids=[];//店铺
        $("#cancelCollect").off("click");
        //取消收藏
        $("#cancelCollect").on("click",function(){
            var imgs=$("#detailshops .chooseImg,#detailgoods .chooseImg,#detailservice .chooseImg");
            var flags=0;
            console.log("当前页"+curr_page);
            if(curr_page==1){//取消商品
                var p_ids=getData_goods();
                console.log(p_ids+"123");
                var cancelGoods=JSON.stringify({
                    "code":"cancelCollect",
                    "data":{
                        "productIds":p_ids,
                        "userId":userId
                    },
                    "version":"1.0"
                });
                getData(url,cancelGoods,cancel_goods_fuc,asyn,type);     
            }else if(curr_page==2){//取消店铺
                var d_shopId=getData_shops();    
                var cancelShops=JSON.stringify({
                    "code":"cancelCollect",
                    "data":{
                        "shopIds":d_shopId,
                        "userId":userId
                    },
                    "version":"1.0"
                });
                getData(url,cancelShops,cancel_shops_fuc,asyn,type);        
            }else if(curr_page==3){//取消服务
                var s_ids=getData_services();    
                var cancelServices=JSON.stringify({
                    "code":"cancelCollect",
                    "data":{
                        "productIds":ser_ids,
                        "userId":userId
                    },
                    "version":"1.0"
                });
                getData(url,cancelServices,cancel_services_fuc,asyn,type);            
            }

            //(取消成功) 刷新，获取收藏的商品信息 
            function cancel_goods_fuc(data){
                if(data.statu==0||data.msg=="成功"){//取消成功
                    console.log("取消成功");
                    $(".edit-e").css("display","block").siblings().css("display","none");
                    editFlag=1;
                    $("#cancelCollect").addClass("hide").removeClass("show").addClass("bggray").removeClass("bgred");
                    getData(url,productCollect,call_func1,asyn,type);
                }
            }
            //(取消成功) 刷新，获取收藏的商家信息
            function cancel_shops_fuc(data){
                if(data.statu==0||data.msg=="成功"){//取消成功
                    console.log("取消成功");
                    $(".edit-e").css("display","block").siblings().css("display","none");
                    editFlag=1;
                    $("#cancelCollect").addClass("hide").removeClass("show").addClass("bggray").removeClass("bgred");   
                    getData(url,shopCollect,call_func3,asyn,type);
                }
            }
            //(取消成功) 刷新，获取收藏的服务信息
            function cancel_services_fuc(data){
                if(data.statu==0||data.msg=="成功"){//取消成功
                    console.log("取消成功");
                    $(".edit-e").css("display","block").siblings().css("display","none");
                    editFlag=1;
                    $("#cancelCollect").addClass("hide").removeClass("show").addClass("bggray").removeClass("bgred");
                    getData(url,serviceCollect,call_func2,asyn,type);
                }
            } 
            //获取需取消的商品id
            function getData_goods(){
                var arr_goods=$("#detailgoods").find(".chooseImg");
                var l=arr_goods.length;
                for(var i=0;i<l;i++){
                    if(arr_goods.eq(i).attr("data-flag")=="1"){
                        pro_ids.push(arr_goods.eq(i).attr("data-proid"));    
                    }   
                }
                return pro_ids;
            }
            //获取需取消的店铺id
            function getData_shops(){
                var arr_shops=$("#detailshops").find(".chooseImg");
                var l=arr_shops.length;
                for(var i=0;i<l;i++){
                    if(arr_shops.eq(i).attr("data-flag")=="1"){
                        shop_ids.push(arr_shops.eq(i).attr("data-shopId"));    
                    }   
                }
                return shop_ids;
            }
            //获取需取消的服务id
            function getData_services(){
                var arr_services=$("#detailservices").find(".chooseImg");
                var l=arr_services.length;
                for(var i=0;i<l;i++){
                    if(arr_services.eq(i).attr("data-flag")=="1"){
                        ser_ids.push(arr_services.eq(i).attr("data-productId"));    
                    }   
                }
                return ser_ids;
            }
        });
    };
    //点击编辑
    $(".edit .edits").on("click",function(){
        var dataStr=$(this).attr("data-edit");
        edit(this);
        if(dataStr.indexOf("edit")>-1){
            $("#cancelCollect").addClass("show").removeClass("hide");
        }else{
            $("#cancelCollect").addClass("hide").removeClass("show");    
        }
    });    
});